package com.guldan.jaina.manager;

import java.util.ArrayList;
import java.util.List;

import com.guldan.core.exception.BusinessException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.guldan.core.manager.BaseManager;
import com.guldan.core.search.Pagination;
import com.guldan.core.utils.DateUtil;
import com.guldan.jaina.dto.area.AreaSearchDTO;
import com.guldan.jaina.dto.dicType.DicTypeDTO;
import com.guldan.jaina.dto.dicType.DicTypeSearchDTO;
import com.guldan.jaina.utils.DicTypeDomainConvertUtils;
import com.guldan.mapper.db.entity.AreaExample;
import com.guldan.mapper.db.entity.DicType;
import com.guldan.mapper.db.entity.DicTypeExample;
import com.guldan.mapper.db.mapper.DicTypeMapperExt;

/**
 * Created by Silas.
 * Date: 2016/11/7
 * Time: 22:55
 */
@Service
public class DicTypeManager extends BaseManager {

    @Autowired
    private DicTypeMapperExt dicTypeMapperExt;

    //获取单个字典类型信息
    public DicTypeDTO get(Integer dicTypeId) {
        return DicTypeDomainConvertUtils.convert(dicTypeMapperExt.selectByPrimaryKey(dicTypeId));
    }

    //判断某个字典是否已经存在
    private Boolean checkExist(String dicTypeName) {
        DicTypeExample example = new DicTypeExample();
        DicTypeExample.Criteria criteria = example.createCriteria();
        criteria.andDicTypeNameEqualTo(dicTypeName);
        criteria.andIsDeleteEqualTo(false);
        int count = dicTypeMapperExt.countByExample(example);
        if (count > 0) {
            return true;
        } else {
            return false;
        }
    }

    //新增/更新单个字典类型，传入：DicTypeDTO
    public DicTypeDTO save(DicTypeDTO dto) throws BusinessException {

        DicType entity;
        if (dto.getDicTypeId() != null && dto.getDicTypeId() > 0) {
            entity = dicTypeMapperExt.selectByPrimaryKey(dto.getDicTypeId());
            entity.setDicTypeName(dto.getDicTypeName());
            entity.setModifyTime(DateUtil.getCurrentTime());
            entity.setModifier(dto.getModifier());
            dicTypeMapperExt.updateByPrimaryKeySelective(entity);
        } else {

            //判断是否存存在
            if (checkExist(dto.getDicTypeName())) {
                throw new BusinessException("字典类别已存在");
            }

            entity = new DicType();
            entity.setDicTypeName(dto.getDicTypeName());
            entity.setCreator(dto.getCreator());
            entity.setCreateTime(DateUtil.getCurrentTime());
            dicTypeMapperExt.insertSelective(entity);
        }
        dto = DicTypeDomainConvertUtils.convert(entity);
        return dto;
    }


    //删除单个字典类型信息
    public Boolean delete(Integer dicTypeId) {
        DicType entity = dicTypeMapperExt.selectByPrimaryKey(dicTypeId);
        entity.setIsDelete(true);
        return dicTypeMapperExt.updateByPrimaryKeySelective(entity) > 0;
    }

    //分页查询字典类型信息列表
    public Pagination<DicTypeDTO> list(DicTypeSearchDTO dicTypeSearchDTO) {
        Pagination<DicTypeDTO> result = new Pagination<DicTypeDTO>(dicTypeSearchDTO.getCurrentPage(), dicTypeSearchDTO.getPerPage());
        DicTypeExample example = buildSearchExample(dicTypeSearchDTO);
        int total = dicTypeMapperExt.countByExample(example);
        result.setTotalCount(total);
        if (total > 0) {
            List<DicTypeDTO> data = new ArrayList<DicTypeDTO>();
            List<DicType> list = dicTypeMapperExt.selectByExampleWithPaging(example, result.getOffset(), result.getPageSize());
            for (DicType entity : list) {
                DicTypeDTO dto = DicTypeDomainConvertUtils.convert(entity);
                data.add(dto);
            }
            result.setList(data);
        }
        return result;
    }

    /**
     * 获取所有指定条件的字典类别
     *
     * @param dicTypeSearchDTO
     * @return
     */
    public List<DicTypeDTO> all(DicTypeSearchDTO dicTypeSearchDTO) {
        DicTypeExample example = buildSearchExample(dicTypeSearchDTO);
        List<DicTypeDTO> data = new ArrayList<DicTypeDTO>();
        List<DicType> list = dicTypeMapperExt.selectByExample(example);
        for (DicType entity : list) {
            DicTypeDTO dto = DicTypeDomainConvertUtils.convert(entity);
            data.add(dto);
        }
        return data;
    }

    //build查询Example
    private DicTypeExample buildSearchExample(DicTypeSearchDTO search) {
        DicTypeExample example = new DicTypeExample();
        DicTypeExample.Criteria criteria = example.createCriteria();

        if (StringUtils.isNotBlank(search.getDicTypeName())) {
            criteria.andDicTypeNameEqualTo(search.getDicTypeName());
        }
        if (search.getDicTypeId() != null && search.getDicTypeId() > 0) {
            criteria.andDicTypeIdEqualTo(search.getDicTypeId());
        }
        criteria.andIsDeleteEqualTo(Boolean.FALSE);
        if (StringUtils.isNotBlank(search.getOrderByClause())) {
            example.setOrderByClause(search.getOrderByClause());
        } else {
            example.setOrderByClause("dic_type_id asc");
        }
        return example;
    }

}
